﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace CosAging
{
    public class iniFileWR
    {
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);
        [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]
        public static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, string lpReturnedString, int nSize, string lpFileName);
        [DllImport("kernel32")]
        public static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
        //;// 指定要写的 INI 文件名
        public List<int> ColorCustom = new List<int>();
        public ColorDialog colordialog = new ColorDialog();
        //;//为初始化文件中指定的条目获取它的整数值
        public static iniFileWR myIniFileOS = new iniFileWR(); //存储打开
        private string strErrorMsg=null;
        public string iniErrorMsg
        {
            get { return strErrorMsg; }
        }

        public string Result { get; set; }
        public string iniFileName { get; set; }
        private string strFileName;
        #region "构造函数"
        public iniFileWR()
        {
            Class_Initialize();
        }
        private void Class_Initialize()
        {
            strFileName = "";
        }
        #endregion

        public bool noIniFile()
        {
            if (strFileName == null)
            {
                string message = "未指定ini文件！";
                string caption = "提示";
                MessageBoxButton buttons = MessageBoxButton.OKCancel;
                System.Windows.MessageBox.Show(message, caption, buttons);
                return true;
            }
            else
            {
                return false;
            }
        }


        public  bool WriteString(string Section, string key, string Value)
        {
            object ErrorMsg; //指定字段，指定键值，写入数据
            if (noIniFile())
            {
                return false;
            }

            if (WritePrivateProfileString(Section, key, Value, iniFileName) == false)
            {
                ErrorMsg = "写入失败";
                return false;
            }
            return true;
        }
        public string ReadString(string Section, string key, int Size)
        {
            try
            {
                string ReturnStr;
                int ReturnLng;
                if (noIniFile())
                {
                    return null;
                }

                ReturnStr = "";
                for (int i = 0; i < Size; i++)
                {
                    ReturnStr = ReturnStr + " ";
                }

                ReturnLng = GetPrivateProfileString(Section, key, "", ReturnStr, Size, iniFileName);
                ReturnStr = ReturnStr.Replace("\0", "").Trim();
                //return MyModule.Left(ReturnStr, ReturnLng);
                return ReturnStr;
            }
           
            catch(Exception ex)
            {
                System.Windows.MessageBox.Show(ex.ToString());
                return "";
            }
        }
        public string ReadString(string Section, string key)
        {
            try
            {
                string ReturnStr;
                int ReturnLng;
                if (noIniFile())
                {
                    return null;
                }

                ReturnStr = "";
                for (int i = 0; i < 32; i++)
                {
                    ReturnStr = ReturnStr + " ";
                }

                ReturnLng = GetPrivateProfileString(Section, key, "", ReturnStr, 32, iniFileName);
                ReturnStr = ReturnStr.Replace("\0", "").Trim();
                //return MyModule.Left(ReturnStr, ReturnLng);
                return ReturnStr;
            }

            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.ToString());
                return "";
            }
        }


        //public void ReadCustomColor()
        //{
        //    int i;
        //    for (i = 0; i <= 15; i++)
        //    {
        //        ColorCustom[i] = Convert.ToInt32(myIniFileOS.ReadString("Colordialog", "CustomColor" + i, 15));
        //    }

        //}

        //public void WriteCustomColor()
        //{
        //    int i;
        //    for (i = 0; i <= 15; i++)
        //    {
        //        ColorCustom[i] = colordialog.CustomColors[i];
        //        if (myIniFileOS.WriteString("Colordialog", "CustomColor" + i, Convert.ToString(ColorCustom[i])) == false)
        //        {
        //            System.Windows.MessageBox.Show("参数" + "CustomColor" + i + "写入ini文件出错", "提示");
        //        }

        //    }

        //}

    }
}
